package com.example.myapplication.BroadcastTest;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.myapplication.ContentTest.ContentActivity;
import com.example.myapplication.LocationTest.LocationActivity;
import com.example.myapplication.MainActivity;
import com.example.myapplication.R;
import com.example.myapplication.Services.ServicesTestActivity;

public class BroadcastTestActivity extends AppCompatActivity {
  private static final String[] songs = { "Song1", "Song2" };
  public static final String ACTION_PLAY = "PLAY";
  public static final String ACTION_PAUSE = "PAUSE";
  public static final String ACTION_STOP = "STOP";
  public static final String ACTION_SWAP = "SWAP";
  private int currentSongIndex = 0;
  private boolean isPlaying = false;
  ImageButton playButton;
  ImageButton stopButton;
  ImageButton nextButton;

  private MusicBroadcastReceiver musicBroadcastReceiver;



  TextView textView;
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.broadcast_layout);
    Toolbar toolbar = findViewById(R.id.toolbar);
    toolbar.setTitle("Broadcast音乐播放器");
    setSupportActionBar(toolbar);

    Init();

  }

  private void Init() {
    textView = findViewById(R.id.music_tip);
    SetTipText(songs[currentSongIndex]);
    playButton = findViewById(R.id.play_button);
    playButton.setImageResource(R.drawable.baseline_play_arrow_24);
    stopButton = findViewById(R.id.stop_button);
    nextButton = findViewById(R.id.next_button);

    playButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        PlayAndPause();
      }
    });
    stopButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        StopPlay();
      }
    });
    nextButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        PlayNext();
      }
    });


    musicBroadcastReceiver=new MusicBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ACTION_PLAY);
    intentFilter.addAction(ACTION_STOP);
    intentFilter.addAction(ACTION_SWAP);
    intentFilter.addAction(ACTION_PAUSE);
    registerReceiver(musicBroadcastReceiver,intentFilter);
  }

  private void PlayNext() {
    Intent intent = new Intent(ACTION_SWAP);
    currentSongIndex = (currentSongIndex + 1) % 2;
    intent.putExtra("songIndex", currentSongIndex);
    sendBroadcast(intent);
    SetTipText(songs[currentSongIndex]);

  }

  private void StopPlay() {
    sendBroadcast(new Intent(ACTION_STOP));
    isPlaying=false;
    playButton.setImageResource(R.drawable.baseline_play_arrow_24);

  }

  @Override protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(musicBroadcastReceiver);

  }

  private void PlayAndPause() {
    if (isPlaying) {
      sendBroadcast(new Intent(ACTION_PAUSE));
      playButton.setImageResource(R.drawable.baseline_play_arrow_24);
    } else {
      Intent intent = new Intent(ACTION_PLAY);
      intent.putExtra("songIndex", currentSongIndex);
      sendBroadcast(intent);
      playButton.setImageResource(R.drawable.baseline_pause_24);
    }
    isPlaying = !isPlaying;
  }

  @Override public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, 1, 0, "返回上一个Activity");
    menu.add(0, 2, 0, "跳转到下一个Activity");
    return true;
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case 1:
        BackToLastActivity();
        break;
      case 2:
        GoToNextActivity();
    }
    return true;
  }

  private void GoToNextActivity() {
    Intent intent = new Intent(this, LocationActivity.class);
    startActivity(intent);
  }
  private void BackToLastActivity() {
    Intent intent = new Intent(this, ServicesTestActivity.class);
    startActivity(intent);

  }

  private void SetTipText(String songName) {
    String newTip = getString(R.string.current_music, songName);
    textView.setText(newTip);

  }
}
